<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/***
	好习惯和坏习惯
**/
class Behave extends CI_Controller {
	
	public function __construct() {
		parent::__construct();
		$this->load->library('session');
		$this->load->model('behave_model');
		$this->load->helper('url');
	}
	//显示所有好习惯和坏习惯
	public function index() {
		isset($_SESSION['user_id']) OR exit('请先登录！');	
		
		$data ['good'] = $this->behave_model->get_good();
		$data ['bad'] = $this->behave_model->get_bad();
		
		
		//加载模版解析类
		$this->load->library('parser');
		
		$this->load->view('templates/fmly_head');		
		$this->parser->parse('behave/list', $data);
		$this->load->view('templates/fmly_foot');
	}
		

	//新增
	public function add($type) {
		isset($_SESSION['user_id']) OR exit('请先登录！');	
		//如果POST传来数据，去增加；
		//否则打开表单
		
		if (isset($_POST['des'])) {
			
			$data = $this->behave_model->add();
			
			//加载习惯列表
			$data ['good'] = $this->behave_model->get_good();
			$data ['bad'] = $this->behave_model->get_bad();
		
		
			//加载模版解析类
			$this->load->library('parser');
			
			$this->load->view('templates/fmly_head');		
			$this->parser->parse('behave/list', $data);
			$this->load->view('templates/fmly_foot');
		}
		else {
			$data ['type'] = $type;
			if ($type == 'good') {
				$data ['title']='新增好习惯';	
			}
			if ($type == 'bad') {
				$data ['title']='新增坏习惯';	
			}
			//加载模版解析类
			$this->load->library('parser');
			$this->load->view('templates/fmly_head');		
			$this->parser->parse('behave/form_add', $data);
			$this->load->view('templates/fmly_foot');
		}
		
	}
	
	
	//删除
	public function del($id = 0) {
		isset($_SESSION['user_id']) OR exit('请先登录！');
		$data = $this->behave_model->del($id);
		
		//加载习惯列表
			$data ['good'] = $this->behave_model->get_good();
			$data ['bad'] = $this->behave_model->get_bad();
		
		
			//加载模版解析类
			$this->load->library('parser');
			
			$this->load->view('templates/fmly_head');		
			$this->parser->parse('behave/list', $data);
			$this->load->view('templates/fmly_foot');
	}
		
}